//
//  ViewController.m
//  03.14-SaveData
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"
#import "StudentModel.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //得到沙盒目录路径
    
    //方式1
    //得到沙盒目录下的文件夹数组
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //Doucuments目录
    NSString *docPath1=paths[0];
    NSLog(@"%@",docPath1);
    
    
    //方式2
    //沙盒目录 NSHomeDirectory()
    NSLog(@"沙盒目录下:%@",NSHomeDirectory());
    NSString *docPath2=[NSHomeDirectory() stringByAppendingString:@"/Documents"];
    
//    NSString *docPath2=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    
    
    NSLog(@"Documents路径:%@",docPath2);
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//1.沙盒模式
- (IBAction)save1:(id)sender {
    NSUserDefaults *d=[NSUserDefaults standardUserDefaults];
    [d setObject:self.idTextField.text forKey:@"stuId"];
    [d setObject:self.nameTextField.text forKey:@"stuName"];
    [d setObject:self.ageTextField.text forKey:@"stuAge"];
    [d synchronize];
}
- (IBAction)read1:(id)sender {
    NSUserDefaults *d=[NSUserDefaults standardUserDefaults];
    
    self.idTextField.text=[d objectForKey:@"stuId"];
    self.nameTextField.text=[d objectForKey:@"stuName"];
    self.ageTextField.text=[d objectForKey:@"stuAge"];
    
}


//2.plist方式
- (IBAction)save2:(id)sender {
    NSDictionary *dict=@{@"stuId":self.idTextField.text,
                         @"stuName":self.nameTextField.text,
                         @"stuAge":self.ageTextField.text};
//    stringByAppendingPathComponent 自动中间加个/
    
    NSString *docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath=[docPath stringByAppendingPathComponent:@"student.plist"];
    NSLog(@"存到:%@",filePath);
    [dict writeToFile:filePath atomically:YES];
}
- (IBAction)read2:(id)sender {
    NSString *docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath=[docPath stringByAppendingPathComponent:@"student.plist"];
    
    NSLog(@"读取:%@",filePath);
    
    NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:filePath];
    
    self.idTextField.text=dict[@"stuId"];
    
    self.nameTextField.text=dict[@"stuName"];
    
    self.ageTextField.text=dict[@"stuAge"];
    
}

//归档方式(数据放入支持归档的对象)
- (IBAction)save3:(id)sender {
    
    NSString *docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Docunments"];
    NSString *filePath=[docPath stringByAppendingPathComponent:@"student.arch"];
    NSLog(@"存到:%@",filePath);
    
    StudentModel *m=[[StudentModel alloc]initWithId:self.idTextField.text andName:self.nameTextField.text andAge:self.ageTextField.text];
    
    
    NSMutableData *mData=[NSMutableData data];
    
    NSKeyedArchiver *arch=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mData];
    [arch encodeObject:m forKey:@"Student"];
    [arch  finishEncoding];
    [mData writeToFile:filePath atomically:YES];
}
- (IBAction)read3:(id)sender {
    NSString *docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath=[docPath stringByAppendingPathComponent:@"student.arch"];
    
    NSData *data=[NSData dataWithContentsOfFile:filePath];
    
    NSKeyedArchiver *unArch=[[NSKeyedUnarchiver alloc]initForReadingWithData:data];
    
    StudentModel *stu=[unArch decodeObjectForKey:@"Student"];
    [unArch finishEncoding];
    
    self.idTextField.text=stu.stuId;
    self.nameTextField.text=stu.stuName;
    self.ageTextField.text=stu.stuAge;
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    [self.idTextField resignFirstResponder];
//    [self.nameTextField resignFirstResponder];
//    [self.ageTextField resignFirstResponder];
    [self.view endEditing:YES];
}
@end
